package com.nnnu.wsnackshop.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nnnu.wsnackshop.pojo.entity.Permissions;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Set;

/**
 * <p>
 * 权限表 Mapper 接口
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@Mapper
public interface PermissionsMapper extends BaseMapper<Permissions> {

    @Select("SELECT p.name FROM permissions p" +
            " JOIN role_permissions rp ON p.id=rp.permission_id" +
            " JOIN user_roles ur ON rp.role_id=ur.role_id" +
            " JOIN users u ON ur.user_id=u.id" + " WHERE u.username=#{username}")
    Set<String> selectPermissionNamesByUsername(@Param("username") String username);


    @Select("""
            SELECT p.id, p.name
              FROM permissions p
              JOIN role_permissions rp ON p.id = rp.permission_id
             WHERE rp.role_id = #{roleId}
            """)
    Set<Permissions> selectByRoleId(@Param("roleId") Long roleId);
}
